<?php
/** 
*
* @package Install
* @version $Id: install_auto_pm.php 9 2007-04-13 23:12:45Z highwayoflife $
* @copyright (c) 2007 Star Trek Guide Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './../../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('common');

if (!$user->data['is_registered'])
{
    if ($user->data['is_bot'])
    {
        redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
    }
    login_box('', 'LOGIN');
}
else if ($user->data['user_type'] != USER_FOUNDER)
{
	trigger_error('NOT_AUTHORISED');
}

/**
 * @todo Convert to normal mods install file, give auto_pm it’s own file
 */
$user->add_lang('mods/auto_pm');

$install = (isset($_REQUEST['install'])) ? true : false;
$uninstall = (isset($_REQUEST['uninstall'])) ? true : false;

$install_action = sprintf($user->lang['INSTALL_MOD'], '<a href="' . append_sid("install_auto_pm.$phpEx", 'install=true') . '">', '</a>');
$uninstall_action = sprintf($user->lang['UNINSTALL_MOD'], '<a href="' . append_sid("install_auto_pm.$phpEx", 'uninstall=true') . '">', '</a>');

$message = '';
$action = $install_action . '<br /><br />' . $uninstall_action;

/**
 * @todo Make the MOD info grab from a specified file from the url, a sort of Hooks system, if you will.
 */
$config_ary = array(
	'pm_user_id'		=> 2,
	'pm_subject'		=> $user->lang['DEFAULT_PM_SUBJECT'],
	'pm_message'		=> $user->lang['DEFAULT_PM_MESSAGE'],
	'board_rules_url'	=> $user->lang['DEFAULT_RULES_URL'],
	'board_rules_title'	=> $user->lang['DEFAULT_RULES_TITLE'],
);

/**
 * @todo Make as a loop for each new table created by the MOD
 */
if (!defined('AUTO_PM_TABLE'))
{
	define('AUTO_PM_TABLE', $table_prefix . 'auto_pm');
}

/**
 * @todo Make into an array, and loop the table installation/uninstallation
 */
define('MOD_TABLE', AUTO_PM_TABLE);

// uninstall the MOD
if ($uninstall)
{
	// drop the table if it exists...
	$db->sql_query('DROP TABLE IF EXISTS ' . MOD_TABLE);
	// tell the user we droped their precious table
	$message .= sprintf($user->lang['TABLE_DELETED'], MOD_TABLE);
}

if ($install)
{
	// Find an exact match
	$result = $db->sql_query('show tables LIKE "' . MOD_TABLE . '"');
	if ($row = $db->sql_fetchrow($result))
	{
		// table found, tell the user it exists already
		$message .= sprintf($user->lang['TABLE_EXISTS'], MOD_TABLE);
	}
	else
	{
		// table not found, proceed to install it...
		$sql = 'CREATE TABLE IF NOT EXISTS ' . MOD_TABLE . ' (
	  autopm_id mediumint(8) unsigned NOT NULL auto_increment,
	  user_id mediumint(8) unsigned NOT NULL default 0,
	  draft_id mediumint(8) unsigned NOT NULL default 0,
	  PRIMARY KEY  (autopm_id),
	  KEY draft_id (draft_id)
	)';
		$db->sql_query($sql);
		
		// tell the user we kindly created the table for them.
		$message .= sprintf($user->lang['TABLE_CREATED'], MOD_TABLE);
	}
	$db->sql_freeresult($result);
	
	// clear the cache so that it does not have any influence on our config values.
	// maybe we can localize this to the global variables?
	$cache->purge();
	
	// loop each of the config values.
	foreach ($config_ary as $key => $value)
	{
		// test the config value to see if it exists...
		if (!isset($config[$key]))
		{
			// it does not exist, so stick it in a SQL array...
			$sql_ary[] = array(
				$key	=> $value,
			);
			
			// tell the user that we are going to create this config value
			/**
			 * @todo This should be moved and only echoed AFTER the config has actually been inserted into the DB
			 */
			$message .= sprintf($user->lang['CONFIG_VALUE_CREATED'], $key, $value);
		}
		else
		{
			// tell the user that the config value already exists.
			$message .= sprintf($user->lang['CONFIG_VALUE_EXISTS'], $key, $value);
		}
	}
	
	// purge the cache again?
	//$cache->purge();
}
trigger_error($message . $action);


?>